package cubelabs.nexus.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import cubelabs.nexus.entity.Message;
import cubelabs.nexus.entity.User;
import cubelabs.nexus.enums.MessageStatus;

@Repository("messageDAO")
public class MessageDAO extends BaseDAO<Message, Long>{
	
	@SuppressWarnings("unchecked")
	public List<Message> findByUser(User user, Date lastMessageDate){
		Criteria crit = getSession().createCriteria(getPersistentClass());
		crit.add(Restrictions.eq("toId", user.getId()));
		crit.add(Restrictions.eq("messageStatus", MessageStatus.CREATED));
		//crit.add(Restrictions.le("creationTime", lastMessageDate));
		crit.addOrder(Order.desc("creationTime"));
		List<Message> messageList = crit.list();
		return messageList;
	}
	
	public Message findBySession(Long sessionId) {
		Criteria crit = getSession().createCriteria(getPersistentClass());
		crit.add(Restrictions.eq("sessionId", sessionId));
		Message result = (Message) crit.uniqueResult();
		return result;
	}
	
}
